Page({
  data:{
    add_name:'',
    add_phone:'',
    add_area:'',
    add_detail:'',
    check_default:'',
    // 由哪个页面跳转来 地址保存后判断页面是否跳转
    fromPage:''
  },
  onLoad(options){
    // console.log(options.fromPage);
    this.setData({
      fromPage:options.fromPage
    })
  },
  // 收货名
  handleAddName(e){
    // console.log(e);
    this.setData({
      add_name:e.detail.value
    })
  },
  handleAddPhone(e){
    // console.log(e);
    this.setData({
      add_phone:e.detail.value
    })
  },
  handleAddArea(e){
    // console.log(e);
    this.setData({
      add_area:e.detail.value
    })
  },
  handleAddDetail(e){
    this.setData({
      add_detail:e.detail.value
    })
  },
  handleChangeDefault(e){
    // console.log(e);
    this.setData({
      add_check:e.detail.value
    })
  },
  handleSave(){
    let that = this;
    if (this.data.add_name == "") {
      wx: wx.showToast({
        title: '请输入姓名',
        icon:'none'
      })
      return false
    }
    else if (this.data.add_phone == "") {
      wx: wx.showToast({
        title: '请输入手机号码',
        icon:'none'
      })
      return false
    }
    else if(this.data.add_area == "") {
      wx: wx.showToast({
        title: '请输入省市区',
        icon:'none'
      })
      return false
    }else if(this.data.add_detail == "") {
      wx: wx.showToast({
        title: '请输入详细地址',
        icon:'none'
      })
      return false
    }else{
      // 获取缓存中地址数据
      let userAddress = wx.getStorageSync("userAddress")||[];
      console.log(userAddress);
      // 获取地址长度 给新添加的地址加id
      let length = userAddress.length;
      let addressParams = {
        add_name:that.data.add_name,
        add_phone:that.data.add_phone,
        add_area:that.data.add_area,
        add_detail:that.data.add_detail,
        add_check:that.data.add_check,
        add_id:length
      }      
      userAddress.push(addressParams);
      // 把地址添加到缓存中
      wx.setStorageSync("userAddress",userAddress);
      // 弹窗显示
      wx.showToast({
        title: '添加成功',
        icon: 'success',
        mask:true
      })
      // 如果是我的地址跳转过来的 页面不跳转 
      // 如果是购物车页面跳转过来 保存后要跳转回购物车页面
      let fromPage = that.data.fromPage;
      console.log(fromPage);
      if(fromPage=='cart'){
        wx.switchTab({
          url: '/pages/cart/cart'
        });
          
      }else if(fromPage=='buy'){
        wx.navigateTo({
          url: '/pages/buy/buy'
        });
      }
    }
  }
})